/**
 * Scoreboard.java
 * 
 * Scoreboard holds the scores of the result of bowler rolls. As ball are
 * rolled, the Scoreboard receives the result of the row and passes them to a
 * Frame to keep the score of that roll.
 * 
 * @author Anthony Reese
 * @author Robert Yates
 */
public class Scoreboard {

	/**
	 * The number of frames that take place for a bowler
	 */
	public static final int MAX_FRAMES = 10;

	/**
	 * The frames that hold the bowlers' scores
	 */
	private Frame[][] frames;

	/**
	 * Creates a scoreboard with a party size
	 */
	public Scoreboard(int partySize) {
		frames = new Frame[partySize][MAX_FRAMES];
		createNewFrames();
	}

	/**
	 * Reset the frames for the current party
	 */
	public void reset() {
		int partySize = frames.length;
		frames = null;
		frames = new Frame[partySize][MAX_FRAMES];
		createNewFrames();
	}

	/**
	 * Create new frames for the scoreboard
	 */
	private void createNewFrames() {
		for (int bowler = 0; bowler < frames.length; bowler++) {
			for (int frame = 0; frame < MAX_FRAMES; frame++) {
				frames[bowler][frame] = new Frame(frame);
			}
		}
	}

	/**
	 * Stores the number of pins that were down as a score for the bowler
	 * 
	 * @param bowler
	 *            who rolled the ball
	 * @param frame
	 *            the frame the ball was roleld in
	 * @param ball
	 *            the ball rolled during the frame (1st, 2nd, or 3rd)
	 * @param pinsDown
	 *            how many pins the ball knocked down
	 */
	public void bowlerRoll(int bowler, int frame, int ball, int pinsDown) {
		frames[bowler][frame].rollBall(pinsDown);
		if (ball == 0 && frame > 0) {
			frames[bowler][frame - 1].linkNextFrame(frames[bowler][frame]);
		}
	}

	/**
	 * Gets the current scores of the score board
	 * 
	 * @return the current scores in the scoreboard
	 */
	public int[][] getScores() {
		int[][] scores = new int[frames.length][MAX_FRAMES];
		for (int bowler = 0; bowler < frames.length; bowler++) {
			int cumalitiveScore = 0;
			for (int frame = 0; frame < MAX_FRAMES; frame++) {
				scores[bowler][frame] = cumalitiveScore
						+ frames[bowler][frame].getScore();
				cumalitiveScore = scores[bowler][frame];
			}
		}
		return scores;
	}
}
